package top.icecola.community.service;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;
import top.icecola.community.dao.DiscussPostMapper;
import top.icecola.community.domain.DiscussPost;
import top.icecola.community.domain.DiscussPostExample;
import top.icecola.community.util.SensitiveFilter;

import java.util.List;

@Service
public class DiscussPostService {
    @Autowired
    private DiscussPostMapper discussPostMapper;

    @Autowired
    private SensitiveFilter sensitiveFilter;

    public List<DiscussPost> pageDiscussPosts(int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        DiscussPostExample discussPostExample = new DiscussPostExample();
        discussPostExample.createCriteria().andStatusNotEqualTo(2);
        discussPostExample.setOrderByClause("type desc,create_time desc");
        return discussPostMapper.selectByExample(discussPostExample);
    }

    public int findDiscussPostRows() {
        DiscussPostExample discussPostExample = new DiscussPostExample();
        discussPostExample.createCriteria().andStatusNotEqualTo(2);
        List<DiscussPost> discussPosts = discussPostMapper.selectByExample(discussPostExample);
        return discussPosts.size();
    }
    public int findUserDiscussPostRows(Integer userId) {
        DiscussPostExample discussPostExample = new DiscussPostExample();
        discussPostExample.createCriteria().andUserIdEqualTo(userId).andStatusNotEqualTo(2);
        List<DiscussPost> discussPosts = discussPostMapper.selectByExample(discussPostExample);
        return discussPosts.size();
    }

    public int addDiscussPost(DiscussPost post) {
        if (post == null) {
            throw new IllegalArgumentException("参数不能为空!");
        }

        // 转义HTML标记
        post.setTitle(HtmlUtils.htmlEscape(post.getTitle()));
        post.setContent(HtmlUtils.htmlEscape(post.getContent()));
        // 过滤敏感词
        post.setTitle(sensitiveFilter.filter(post.getTitle()));
        post.setContent(sensitiveFilter.filter(post.getContent()));
        post.setCommentCount(0);

        return  discussPostMapper.insert(post);
    }


    public DiscussPost findDiscussPostById(int discussPostId) {
        return discussPostMapper.selectByPrimaryKey(discussPostId);
    }

//    修改评论数量
    public  void updateDiscussPostComment(int entityId,int commentCount){
        DiscussPost discussPost = findDiscussPostById(entityId);
        discussPost.setCommentCount(commentCount);
        DiscussPostExample discussPostExample = new DiscussPostExample();
        discussPostExample.createCriteria().andIdEqualTo(entityId);
        discussPostMapper.updateByExampleSelective(discussPost,discussPostExample);
    }

    public List<DiscussPost> addAllDiscuss() {
        DiscussPostExample discussPostExample = new DiscussPostExample();
        return discussPostMapper.selectByExample(discussPostExample);
    }

    public void setType(Integer postId,Integer type){
        DiscussPostExample discussPostExample = new DiscussPostExample();
        DiscussPost discussPost = findDiscussPostById(postId);
        discussPost.setType(type);
        discussPostExample.createCriteria().andIdEqualTo(postId);
        discussPostMapper.updateByExampleSelective(discussPost,discussPostExample);
    }

    public void setState(Integer postId,Integer state){
        DiscussPostExample discussPostExample = new DiscussPostExample();
        DiscussPost discussPost = findDiscussPostById(postId);
        discussPost.setStatus(state);
        discussPostExample.createCriteria().andIdEqualTo(postId);
        discussPostMapper.updateByExampleSelective(discussPost,discussPostExample);
    }

    public Integer deletePostRows(){
        DiscussPostExample discussPostExample = new DiscussPostExample();
        discussPostExample.createCriteria().andStatusEqualTo(2);
        List<DiscussPost> postList = discussPostMapper.selectByExample(discussPostExample);
        return postList.size();
    }
    public List<DiscussPost> deletePost(int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        DiscussPostExample discussPostExample = new DiscussPostExample();
        discussPostExample.createCriteria().andStatusEqualTo(2);
        return discussPostMapper.selectByExample(discussPostExample);
    }
}
